<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="员工名称" prop="dictName">
          <el-input
            v-model="queryParams.data.name"
            placeholder="请输入员工名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="dictType">
          <el-input
            v-model="queryParams.data.phoneNumber"
            placeholder="请输入员工手机号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

       <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="el-icon-edit"-->
<!--            size="mini"-->
<!--            :disabled="single"-->
<!--            @click="handleUpdate"-->
<!--            v-hasPermi="['system:dict:edit']"-->
<!--          >修改</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="el-icon-delete"-->
<!--            size="mini"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['system:dict:remove']"-->
<!--          >删除</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="el-icon-download"-->
<!--            size="mini"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['system:dict:export']"-->
<!--          >导出</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="el-icon-refresh"-->
<!--            size="mini"-->
<!--            @click="handleRefreshCache"-->
<!--            v-hasPermi="['system:dict:remove']"-->
<!--          >刷新缓存</el-button>-->
<!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="员工ID" align="center" prop="employeeId" />
        <el-table-column label="员工姓名" align="center" prop="name" />
        <el-table-column label="岗位名称" align="center" prop="jobName" width="180" />
        <el-table-column label="联系电话" align="center" prop="tel" />
        <el-table-column label="邮箱" align="center" prop="email" />
        <el-table-column label="二维码" align="center" prop="qrcodeUrl" width="180" >
	<template slot-scope="scope">
  	<el-popover placement="top-start" trigger="hover">
    	<img :src="scope.row.qrcodeUrl.cover" alt="" style="width: 150px;height: 150px">
      <img slot="reference" :src="scope.row.qrcodeUrl" style="width: 80px;height: 80px">
    </el-popover>
  </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="downDr(scope.row)"
            >下载名片二维码</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
      />

      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" placeholder="请输入姓名" />
              </el-form-item>
              <el-form-item label="联系电话" prop="tel">
                <el-input
                  v-model="form.tel"
                  placeholder="请输入联系电话"
                  maxlength="11"
                />
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input
                  v-model="form.email"
                  placeholder="请输入邮箱"
                  maxlength="50"
                />
              </el-form-item>
              <el-form-item label="在职岗位" prop="jobName">
                <el-input
                  v-model="form.jobName"
                  placeholder="请输入在职岗位"
                />
              </el-form-item>
              <el-form-item label="二维码指向链接" prop="qrcode">
                <el-input
                  v-model="form.qrcode"
                  placeholder="请输入二维码链接"
                />
              </el-form-item>
              <el-form-item label="二维码" prop="qrcodeUrl">
                <div>

                  <upload-view v-model="form.qrcodeUrl" @img="form.qrcodeUrl"></upload-view>
                </div>
              </el-form-item>
          <!-- <el-row>
            <el-col :span="12">
              <el-form-item label="二维码" prop="email">
                <el-input
                  v-model="form.detailAddress"
                  placeholder="请输入邮箱"
                  maxlength="50"
                />
              </el-form-item>
            </el-col>
          </el-row> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>


    </div>
  </template>

  <script>
  import {
    listType3,
    getType,
    delType,
    addType,
    updateType,
    refreshCache,
    peopleupdateType, employeeAddAndUpdate, getEmployee, delEmployee
  } from '@/api/system/dict/type'
  import uploadView from '@/components/ImageUpload/index.vue'

  export default {
    name: "Dict",
    components: { uploadView },
    dicts: ['sys_normal_disable'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 字典表格数据
        typeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
            data: {
            name: "",
            phoneNumber:""
        },
          current: 1,
          size: 10,
          name:''
        },
        // 表单参数
        form: {},
        // 表单校验
        // 表单校验
        rules: {
          name: [
            { required: true, message: "姓名不能为空", trigger: "blur" }
          ],
          tel: [
            { required: true, message: "联系电话不能为空", trigger: "blur" }
          ],
          email: [
            { required: true, message: "邮箱不能为空并且注意格式", trigger: "blur",pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ }
          ],
          jobName: [
            { required: true, message: "在职岗位不能为空", trigger: "blur" }
          ],
          qrcode: [
            { required: true, message: "二维码指向链接不能为空", trigger: "blur" }
          ],
          qrcodeUrl: [
            { required: true, message: "二维码地址不能为空", trigger: "blur" }
          ]
        }

      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询字典类型列表 */
      getList() {
        this.loading = true;
        listType3(this.queryParams).then(response => {
            this.typeList = response.data.records;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dictId: undefined,
          dictName: undefined,
          dictType: undefined,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.current = 1;
        this.getList();
      },
      downDr(row){
        window.location.href = row?.qrcodeUrl ?? null
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加字典类型";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dictId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const employeeId = row.employeeId || this.ids
        getEmployee(employeeId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改员工信息";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.employeeId != undefined) {
              employeeAddAndUpdate(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              employeeAddAndUpdate(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dictIds = row.employeeId || this.ids;
        this.$modal.confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？').then(function() {
          return delEmployee(dictIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/dict/type/export', {
          ...this.queryParams
        }, `type_${new Date().getTime()}.xlsx`)
      },
      /** 刷新缓存按钮操作 */
      handleRefreshCache() {
        refreshCache().then(() => {
          this.$modal.msgSuccess("刷新成功");
          this.$store.dispatch('dict/cleanDict');
        });
      }
    }
  };
  </script>
